﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using NextCMS.Models;

public partial class WebAdmin_AddTemplate : WebadminBasePage
{
    int Id = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        int.TryParse(Convert.ToString(Request.QueryString["Id"]), out Id);
        if (!IsPostBack)
        {
            if (Id == 0)
            {
                BindDropDown();
                step1.Visible = true;
                step2.Visible = false;
                ltrtitle.Text = "Add New Template";
            }
            else
            {
                using (TemplatesHelper cc = new TemplatesHelper())
                {
                    TemplateModel cModel = cc.GetById(Id);
                    step1.Visible = false;
                    step2.Visible = true;
                    ltrtitle.Text = "Template: " + cModel.Name;
                    using (SiteResourcesHelper helper = new SiteResourcesHelper())
                    {
                       SiteResourceModel siteResourceModel=  helper.GetById(cModel.WebResourceId);
                       if (siteResourceModel != null)
                       {
                           Session["FileManager"] = "/Templates/" + siteResourceModel.FolderName + "/";
                           ltrViewFiles.Text = @"<iframe frameborder='0' src='../FileManager/Default.aspx?sessionid=" + Session.SessionID + "' style='height: 430px; width: 100%'></iframe>";
                       }
                    }
                }
            }
        }
    }
    void BindDropDown()
    {
        using (SiteResourcesHelper helper = new SiteResourcesHelper())
        {
            DDLWebResource.DataSource= helper.GetAllBySiteId(SiteId);
            DDLWebResource.DataTextField = "Name";
            DDLWebResource.DataValueField = "Id";
            DDLWebResource.DataBind();
        }
    }
    protected void btnAddTemplate_Click(object sender, EventArgs e)
    {
        TemplatesHelper cc = new TemplatesHelper();
        TemplateModel cModel = new TemplateModel();
        cModel.Name = txtMasterPageName.Text;

        cModel.WebResourceId =Convert.ToInt32( DDLWebResource.SelectedValue);
        if (FUTemplate.HasFile && FUTemplate.FileName != "")
        {
            string fileName = Guid.NewGuid() + Path.GetExtension(FUTemplate.FileName);
            string filePath = Server.MapPath("~/Uploads/Images/") + fileName;
            cModel.TemplateImageName = fileName;
            ResizeImage(filePath, 120, 90, FUTemplate.FileContent);
        }
        cModel.SiteId = SiteId;
        Id = cc.Save(cModel);
        if (Id > 0)
            Response.Redirect("AddTemplate.aspx?Id=" + Id + "");

    }
    
    protected void btnDone_Click(object sender, EventArgs e)
    {
        Response.Redirect("../TemplateEdit.aspx?Id="+Id+"&Type=AddTemplate");
    }
}